﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Enterprise.Service.Translation
{
    /// <summary>
    /// 语言翻译类
    /// </summary>
    public class Translate
    {
        #region 属性/变量

        /// <summary>
        /// 缓存Key
        /// </summary>
        private static string TCacheKey = "FIOCLanguageDictionary";

        /// <summary>
        /// 通过webconfig启用缓存
        /// </summary>
        private static readonly bool UseCache = bool.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["DictionaryCache"]);

        /// <summary>
        /// 我的字典
        /// </summary>
        private static List<WebUIString> MyDictionary
        {
            get
            {
                if (UseCache)
                    return GetMyDictionaryFromCache();
                else
                    return GetList();
            }
        }

        #endregion

        #region ===========>公共方法Translate

        /// <summary>
        /// 将中文翻译成对应语言的文字 。Language可扩展
        /// 此处采用了缓存 如需更新缓存请调用UpdateCache来更新字典缓存
        /// </summary>
        /// <param name="p">字符串</param>
        /// <param name="lang">语种</param>
        /// <returns>翻译后的字符串</returns>
        public static string To(string p, Language lang)
        {
            string EnString = p;
            var q = MyDictionary.Where(s => s.Zwmc == p && s.YZ == lang.ToString()).FirstOrDefault();
            if (q != null)
            {
                EnString = q.Dymc;
            }
            return EnString;
        }

        /// <summary>
        /// 翻译页面
        /// </summary>
        /// <param name="page"></param>
        public static void To(System.Web.UI.Page page,Language lang)
        {
            Control control = (Control)page;
            TransLation(control,lang);
        }

       

        /// <summary>
        /// 从常量字典表中获取字符串
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static string GetString(string p)
        {
            string rtnStr = p;
            if (!string.IsNullOrEmpty(HttpContext.Current.Session.SessionID))
            {
                if (HttpContext.Current.Session["language"] != null)
                {
                    try
                    {
                        Language lang = (Language)Enum.Parse(typeof(Language), HttpContext.Current.Session["language"].ToString());
                        rtnStr = To(p, lang);
                    }
                    catch
                    { }
                }
            }
            return rtnStr;
        }

        /// <summary>
        /// 更新字典缓存
        /// </summary>
        public static void UpdateCache()
        {
            HttpContext.Current.Cache.Remove(TCacheKey);
        }

        #endregion

        #region ================>私有方法
        /// <summary>
        /// 获取中英字典
        /// </summary>
        /// <returns></returns>
        private static List<WebUIString> GetMyDictionaryFromCache()
        {
            if (System.Web.HttpContext.Current.Cache[TCacheKey] != null)
            {
                return (List<WebUIString>)System.Web.HttpContext.Current.Cache[TCacheKey];
            }
            else
            {
                return GetList();
            }
        }



        /// <summary>
        /// 字典
        /// </summary>
        /// <returns></returns>
        private static List<WebUIString> GetList()
        {
            List<WebUIString> list = new List<WebUIString>();
            Enterprise.Data.Translation.TranslateData data = new Data.Translation.TranslateData();
            DataSet ds = data.GetDataSet();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                WebUIString webuistring = new WebUIString();
                webuistring.Zwmc = (string)dr["ZWMC"];
                webuistring.YZ = (string)dr["YZ"];
                webuistring.Dymc = dr["DYMC"].ToString();
                list.Add(webuistring);
            }
            return list;
        }

        /// <summary>
        /// 翻译控件
        /// </summary>
        /// <param name="parentControl"></param>
        private static void TransLation(Control parentControl, Language lang)
        {
            if (!parentControl.HasControls())
            {
                return;
            }
            foreach (Control control in parentControl.Controls)
            {
                if (control.HasControls())
                {
                    //递归
                    TransLation(control, lang);
                }
                switch (control.GetType().Name.ToString())
                {
                    case "Label":
                        Label label = (Label)control;
                        label.Text = Translate.To(label.Text, lang);
                        break;
                    case "Button":
                        Button button = (Button)control;
                        button.Text = Translate.To(button.Text, lang);
                        break;
                    case "Literal":
                        Literal literal = (Literal)control;
                        literal.Text = Translate.To(literal.Text, lang);
                        break;
                    case "LinkButton":
                        LinkButton linkbutton = (LinkButton)control;
                        linkbutton.Text = Translate.To(linkbutton.Text, lang);
                        break;
                    case "DataControlFieldHeaderCell":
                        DataControlFieldHeaderCell dch = (DataControlFieldHeaderCell)control;
                        dch.Text = Translate.To(dch.Text, lang);
                        break;
                    case "HyperLink":
                        HyperLink hlink = (HyperLink)control;
                        hlink.Text = Translate.To(hlink.Text, lang);
                        break;
                    default:
                        break;
                }

            }
        }

        #endregion
    }
}
